const isPromise = promise => {
    if ((typeof promise === 'object' && typeof promise !== null) || typeof promise === 'function') {
        return typeof promise.then === 'function'
    } else {
        return false
    }
}

const p1 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve(100)
    }, 3000)
})
const p2 = new Promise((resolve, reject) => {
    setTimeout(() => {
        reject(100)
    }, 1000)
})

Promise.allSettled([p1, p2, 3]).then(data => {
    console.log(data)
})
/**
 * 不论执行结果如何，最终都会获取到
 * @param {*} promises 
 * @returns 
 */
Promise.allSettled = function (promises) {
    return new Promise((resolve, reject) => {
        const result = []
        const len = promises.length;
        for (let i = 0; i < len; i++) {
            const currentPromise = promises[i];
            Promise.resolve(currentPromise).then(data => {
                result.push({
                    status: 'fulfilled',
                    value: data
                })
                len -= 1;
                if(len === 0) {
                    resolve(result)
                }
            }, err => {
                result.push({
                    status: 'rejected',
                    reason: err
                })
                len -= 1;
                if(len === 0) {
                    resolve(result)
                }
            })
        }
    })
}